﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MediaManager
{
    public partial class DelFrm : Form
    {
        private DelFrm() 
        {
            InitializeComponent();
        }
        public DelFrm(string userName)
        {
            InitializeComponent();
            _userName = userName;
        }
        private string _userName = "";
        private void DelFrm_Load(object sender, EventArgs e)
        {
            try
            {

                Init();
            }
            catch (Exception ex)
            {
                GlobalBase.CatchException.DealWithException(ex);
            }

        }

        private void Init()
        {
            var loginV = (from p in _dataContext.Login
                          where p.name != _userName
                          select new
                          {
                              编号 = p.id,
                              登陆名 = p.name,
                              创建日期 = p.创建日期
                          }).ToList();
            dataGridView1.DataSource = loginV;
            dataGridView1.Refresh();
        }
        DataClasses2DataContext _dataContext = new DataClasses2DataContext();

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.DataSource == null)
                {
                    GlobalBase.Global.Instance.ShowMessage("请先查询数据！");
                    return;
                }
                int j = dataGridView1.CurrentRow.Index;
                if (j < 0)
                {
                    GlobalBase.Global.Instance.ShowMessage("请先选中要删除的数据行！");
                    return;
                }
                int id = Convert.ToInt32( dataGridView1.Rows[j].Cells[0].Value);
                string s = "确实删除编号为：{0}的这条数据吗？";
                s = string.Format(s, id);
                if (MessageBox.Show(s, "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Login login = _dataContext.Login.Where(p => p.id == id).Single<Login>();
                    _dataContext.Login.DeleteOnSubmit(login);
                    _dataContext.SubmitChanges();
                    string sss = string.Format(StructDayInfo.删除人员, dataGridView1.Rows[j].Cells[1].Value, DateTime.Now);
                    InserDayInfo(sss);
                    Init();
                }
                else
                {
                    throw new Exception("提取数据错误！");
                }

            }
            catch (Exception ex)
            {
                GlobalBase.CatchException.DealWithException(ex);
                GlobalBase.Global.Instance.ShowMessage(ex.Message);
            }
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            btnDel_Click(sender, e);
        }
        private void InserDayInfo(string dInfo)
        {
            DayInfo dayInfo = new DayInfo();
            dayInfo.id = _dataContext.GetDayInfoIndex();
            dayInfo.name = _userName;
            dayInfo.操作 = dInfo;
            dayInfo.日期 = DateTime.Now;
            _dataContext.DayInfo.InsertOnSubmit(dayInfo);
            _dataContext.SubmitChanges();

        }
    }
}
